package com.edu.labmanager.controller;


import com.edu.labmanager.common.MyUserDetails;
import com.edu.labmanager.entity.College;
import com.edu.labmanager.entity.LabAdminManagement;
import com.edu.labmanager.entity.Laboratory;
import com.edu.labmanager.entity.Term;
import com.edu.labmanager.service.ICollegeService;
import com.edu.labmanager.service.ILabAdminManagementService;
import com.edu.labmanager.service.ILaboratoryService;
import com.edu.labmanager.service.ITermService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 实验室表，存储学校各实验室的基本信息及所属学院 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-06-15
 */
@Controller
@RequestMapping("/laboratory")
public class LaboratoryController {
    @Autowired
    private ILaboratoryService laboratoryService;

    @Autowired
    private ICollegeService collegeService;
    @Autowired
    private ILabAdminManagementService iLabAdminManagementService;
    @Autowired
    private ITermService termService;

    // 列表页
    @GetMapping("/")
    @PreAuthorize("hasRole('admin')")
    public String listLaboratories(Model model) {
        List<Laboratory> laboratories = laboratoryService.list();
        model.addAttribute("laboratories", laboratories);
        Map<Integer, String> mapCollege = collegeService.list().stream().collect(Collectors.toMap(College::getCollegeId, College::getCollegeName));
        model.addAttribute("mapCollege",mapCollege);
        return "laboratory-list";
    }
    @GetMapping("/Info")
    public String showInfo(Model model, Authentication authentication) {
        MyUserDetails userDetails = (MyUserDetails) authentication.getPrincipal();
        model.addAttribute("user", userDetails);
        List<Integer> labIds = iLabAdminManagementService.query().eq("user_id", userDetails.getId()).list()
                .stream().map(LabAdminManagement::getLaboratoryId).collect(Collectors.toList());
        List<Laboratory> laboratories = Collections.emptyList();

        if (!labIds.isEmpty()) {
            laboratories = laboratoryService.listByIds(labIds);
        }

        model.addAttribute("termMap",termService.list().stream().collect(Collectors.toMap(Term::getTermId, Term::getTermName)));
        model.addAttribute("labMap",laboratories.stream().collect(Collectors.toMap(Laboratory::getLaboratoryId, Laboratory::getLaboratoryName)));
        return "laboratory-info";
    }

    // 新增页面
    @GetMapping("/add")
    @PreAuthorize("hasRole('admin')")
    public String showAddForm(Model model) {
        model.addAttribute("laboratory", new Laboratory());
        model.addAttribute("colleges", collegeService.list());
        return "laboratory-form";
    }

    // 提交新增
    @PostMapping("/add")
    @PreAuthorize("hasRole('admin')")
    public String addLaboratory(@ModelAttribute Laboratory laboratory) {
        laboratoryService.save(laboratory);
        return "redirect:/laboratory/";
    }

    // 编辑页面
    @GetMapping("/edit/{id}")
    @PreAuthorize("hasRole('admin')")
    public String showEditForm(@PathVariable Integer id, Model model) {
        Laboratory lab = laboratoryService.getById(id);
        model.addAttribute("laboratory", lab);
        model.addAttribute("colleges", collegeService.list());
        return "laboratory-form";
    }

    // 提交更新
    @PostMapping("/update")
    @PreAuthorize("hasRole('admin')")
    public String updateLaboratory(@ModelAttribute Laboratory laboratory) {
        laboratoryService.updateById(laboratory);
        return "redirect:/laboratory/";
    }

    // 删除实验室
    @GetMapping("/delete/{id}")
    @PreAuthorize("hasRole('admin')")
    public String deleteLaboratory(@PathVariable Integer id) {
        laboratoryService.removeById(id);
        return "redirect:/laboratory/";
    }

}
